package com.ibm.ems.servlets;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.ems.dao.SearchEventsDAO;
import com.ibm.ems.dto.EventBean;
import com.ibm.ems.dto.UserBean;

/**
 * Servlet implementation class SearchEvents
 */
public class SearchEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SearchEventServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SearchEventsDAO rd=new SearchEventsDAO();
		int flag=1;
		if(request.getParameter("username")!=null){
			String eventid=request.getParameter("eventid");
			String username=request.getParameter("username");
			EventBean eb1=new EventBean();
			UserBean ub1=new UserBean();
			eb1.setEventId(eventid);
			ub1.setUserName(username);
			System.out.println("inifcondition");
			try {
				flag=rd.makeEnroll(eb1,ub1);
			} catch (SQLException e1) {
				
				e1.printStackTrace();
			}
			request.setAttribute("p", eb1);
			request.setAttribute("r", ub1);
			if(flag==1){
				request.getRequestDispatcher("enrollsuccess.jsp").forward(request, response);  
			}else{
				request.getRequestDispatcher("enrollfails.jsp").forward(request, response);  
			}
		
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String Sortby=request.getParameter("searchby");
		System.out.println("Servlet");
		SearchEventsDAO rd=new SearchEventsDAO();
		//int flag=1;
		/*
		if(request.getParameter("username")!=null){
			String eventid=request.getParameter("eventid");
			String username=request.getParameter("username");
			EventBean eb1=new EventBean();
			UserBean ub1=new UserBean();
			eb1.setEventid(eventid);
			ub1.setUserName(username);
			System.out.println("inifcondition");
			try {
				flag=rd.makeEnroll(eb1,ub1);
			} catch (SQLException e1) {
				
				e1.printStackTrace();
			}
			request.setAttribute("p", eb1);
			request.setAttribute("r", ub1);
			if(flag==1){
				request.getRequestDispatcher("enrollsuccess.jsp").forward(request, response);  
			}else{
				request.getRequestDispatcher("enrollfails.jsp").forward(request, response);  
			}
		
		}*/
		
		
		
		
		
		try{
			ArrayList<EventBean> l=new ArrayList<EventBean>();
		if(Sortby.equals("eventname"))
		{
			rd.orderByName(l);
		}
		else if(Sortby.equals("eventid")){
			rd.orderById(l);
		}
		else if(Sortby.equals("startdate")){
			rd.orderByDate(l);
			
		}
		RequestDispatcher dispatch=request.getRequestDispatcher("ViewPage.jsp");
		request.setAttribute("el",l);
		dispatch.forward(request, response);	
		
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		}
	
		//----------
		 
		 
	
		
	}
	
}
